<script setup>
import { ref } from 'vue'
import { shopItemService } from '@/services/shop_item'
import { imagePath } from '@/utils/image'
import { userManager } from '@/utils/user'
import { ElMessage } from 'element-plus'
import { creditManager } from '@/utils/credit'
import creditService from '@/services/creditService'
import { methods } from '@/utils/methods'

const userPoints = ref(creditManager.getCurrentCredit()) // 用户当前积分
const totalPoints = ref(creditManager.getTotalCredit()) // 用户累计积分

// 商品数据
const products = ref([
  {
    id: "1",
    title: 'VIP会员月卡',
    price: 800,
    image: 'https://placeholder.pics/svg/150x150/DEDEDE/555555/VIP',
    inventory: 12,
    description: '享受30天VIP特权，无广告浏览'
  },
  {
    id: "2",
    title: 'VIP会员月卡',
    price: 800,
    image: 'https://placeholder.pics/svg/150x150/DEDEDE/555555/VIP',
    inventory: 12,
    description: '享受30天VIP特权，无广告浏览'
  },
])

shopItemService.getShopItems((res) => {
  console.log(res)
  products.value = res.data
})
// 兑换商品
const redeemProduct = (productId) => {
  if (userManager.getUserInfo() == null) {
    userManager.toLogin()
    ElMessage.error("请先登录")
    return 
  }

  const product = products.value.find(p => p.id === productId)

  if (product && userPoints.value >= product.price) {
    shopItemService.decreaseShopItem(productId, 1, (res) => {
       if(res.code ===1000)
       {
               product.inventory -= 1
      ElMessage.success(`成功兑换 ${product.title}！`)

      creditService.addNewCredit(-product.price,null,methods.POINTS_MALL,userManager.getUserId(),(res)=>{
      let updatedPrice=shopItemService. updateCredit(-product.price)
      userPoints.value = userPoints.value+updatedPrice
     // totalPoints.value = totalPoints.value+updatedPrice
      creditManager.setCurrentCredit(userPoints.value)
     // creditManager.setTotalCredit(totalPoints.value)
    })
       }
       else{
        ElMessage.error(res.msg)
       }

     
    })
    
  } else if (product) {
    alert('积分不足，无法兑换')
  }
}
</script>

<template>
  <div class="points-mall-page">
    <!-- 顶部积分信息栏，仅在用户登录时显示 -->
    <div class="points-header" v-if="userManager.getUserInfo() != null">
      <div class="points-info">
        <div class="points-item">
          <span class="points-label">当前学分:</span>
          <span class="points-value">{{ userPoints }}</span>
        </div>
        <div class="points-item">
          <span class="points-label">累积学分:</span>
          <span class="points-value">{{ totalPoints }}</span>
        </div>
      </div>
      <button class="earn-button" @click="$router.push('/earn-points')">获取学分</button>
    </div>

    <!-- 商品展示区 -->
    <h1 class="mall-title">积分商城</h1>
    <p class="mall-subtitle">用学习积累的积分兑换心仪商品</p>

    <div class="product-grid">
      <div 
        class="product-card" 
        v-for="product in products" 
        :key="product.id"
        :class="{ 'out-of-stock': product.inventory <= 0 }"
      >
        <div class="product-image">
          <img :src="imagePath.getServerImagePath(product.image)" :alt="product.title">
          <div v-if="product.inventory <= 0" class="sold-out">已售罄</div>
        </div>
        <div class="product-info">
          <h3 class="product-name">{{ product.title }}</h3>
          <p class="product-desc">{{ product.description }}</p>
          <div class="product-footer">
            <span class="product-points">{{ product.price }} 积分</span>
            <span class="product-stock">剩余: {{ product.inventory }}</span>
          </div>
          <button 
            class="redeem-button" 
            @click="redeemProduct(product.id)"
            :disabled="product.inventory <= 0 || (userManager.getUserInfo() != null && userPoints < product.price)"
          >
            {{ product.inventory <= 0 ? '已售罄' : '立即兑换' }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* 样式保持不变 */
.points-mall-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

/* 顶部积分信息栏 */
.points-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(135deg, #f1f8ff 0%, #d6f7ff 100%);
  padding: 15px 25px;
  border-radius: 12px;
  margin-bottom: 30px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.points-info {
  display: flex;
  gap: 30px;
}

.points-item {
  display: flex;
  align-items: center;
}

.points-label {
  font-size: 16px;
  color: #8c6b5a;
  margin-right: 10px;
}

.points-value {
  font-size: 24px;
  font-weight: bold;
  color: #ff6b25;
}

/* 获取学分按钮 */
.earn-button {
  padding: 12px 24px;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  font-size: 16px;
  letter-spacing: 0.5px;
  background: linear-gradient(135deg, #04cff3 0%, #0344f8 100%);
  color: white;
  box-shadow: 0 4px 10px rgba(248, 154, 3, 0.2);
}

.earn-button:hover {
  background: linear-gradient(135deg, #0344f8 0%, #04cff3 100%);
  transform: translateY(-2px);
  box-shadow: 0 6px 15px rgba(248, 154, 3, 0.3);
}

/* 商城标题 */
.mall-title {
  text-align: center;
  font-size: 32px;
  color: #333;
  margin-bottom: 10px;
  font-weight: 600;
}

.mall-subtitle {
  text-align: center;
  font-size: 16px;
  color: #666;
  margin-bottom: 40px;
}

/* 商品网格布局 */
.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 25px;
  margin-top: 20px;
}

/* 商品卡片样式 */
.product-card {
  background-color: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
}

.product-card.out-of-stock {
  opacity: 0.7;
}

.product-image {
  position: relative;
  height: 180px;
  background-color: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
}

.product-image img {
  max-width: 80%;
  max-height: 80%;
  object-fit: contain;
}

.sold-out {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: bold;
}

.product-info {
  padding: 20px;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
}

.product-name {
  font-size: 18px;
  color: #333;
  margin-bottom: 10px;
  font-weight: 600;
}

.product-desc {
  font-size: 14px;
  color: #666;
  margin-bottom: 15px;
  line-height: 1.4;
  flex-grow: 1;
}

.product-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.product-points {
  font-size: 18px;
  font-weight: bold;
  color: #ff6b25;
}

.product-stock {
  font-size: 14px;
  color: #888;
}

/* 兑换按钮 */
.redeem-button {
  padding: 10px;
  border: none;
  border-radius: 6px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s;
  width: 100%;
  background-color: #e4e41b;
  color: white;
}

.redeem-button:hover:not(:disabled) {
  background-color: #fe9f07;
  transform: translateY(-2px);
}

.redeem-button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .points-header {
    flex-direction: column;
    gap: 15px;
    align-items: flex-start;
  }

  .earn-button {
    width: 100%;
  }

  .product-grid {
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  }
}

@media (max-width: 480px) {
  .points-info {
    flex-direction: column;
    gap: 10px;
  }

  .mall-title {
    font-size: 26px;
  }

  .mall-subtitle {
    font-size: 14px;
  }
}
</style>